﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace CoffeePotServer
{
    public class Server_Response
    {
        #region Attributes
        private NetworkStream _clientStream;
        #endregion

        #region Constructor
        public Server_Response(NetworkStream thisNetStream)
        {
            _clientStream = thisNetStream;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Send a message to the client.
        /// </summary>
        /// <param name="strMessage">The message to send</param>
        public void SendLine(String strMessage)
        {
            _clientStream.Write(ConvertStringToByteArray(strMessage), 0, strMessage.Length);
        }

        /// <summary>
        /// Send a message to the client.
        /// </summary>
        /// <param name="strMessage">The message to send in plain format</param>
        public void SendLine(byte[] byteArrMessage)
        {
            _clientStream.Write(byteArrMessage, 0, byteArrMessage.Length);
        }

        /// <summary>
        /// Create a byte array from a given string.
        /// </summary>
        /// <param name="strToConvert">The string to convert</param>
        /// <returns>A byte array representation of the given string</returns>
        private byte[] ConvertStringToByteArray(String strToConvert)
        {
            byte[] byteBuff = new byte[strToConvert.Length];
            char[] charBuff = strToConvert.ToCharArray();
            for (int intCount = 0; intCount < charBuff.Length; intCount++)
            {
                byteBuff[intCount] = Convert.ToByte(charBuff[intCount]);
            }
            return byteBuff;
        }
        #endregion
    }
}
